import i18n from '@/lang';

/**
 * @description 全局过滤器：将秒数转成-> x min、xx min、xx hr xx min、xx day xx hr xx min
 * @param {number} seconds 秒数
 * @returns {string} x min、xx min、xx hr xx min、xx day xx hr xx min
 */
export const secondsToTime = (seconds) => {
    if (seconds < 60 || !seconds) return `0 ${i18n.t('common.min_text')}`;

    const ONE_DAY_IN_SECONDS = 86400;
    const ONE_HOUR_IN_SECONDS = 3600;
    const ONE_MINUTE_IN_SECONDS = 60;

    const days = Math.floor(seconds / ONE_DAY_IN_SECONDS);
    seconds -= days * ONE_DAY_IN_SECONDS;
    const hours = Math.floor(seconds / ONE_HOUR_IN_SECONDS);
    seconds -= hours * ONE_HOUR_IN_SECONDS;
    const minutes = Math.floor(seconds / ONE_MINUTE_IN_SECONDS);

    const timeSegments = [];

    if (days) timeSegments.push(`${days.toString().padStart(2, '0')} ${i18n.t('common.day_text')}`);
    if (hours) timeSegments.push(`${hours.toString().padStart(2, '0')} ${i18n.t('common.hr_text')}`);
    if (minutes) timeSegments.push(`${minutes.toString().padStart(2, '0')} ${i18n.t('common.min_text')}`);

    return timeSegments.join(' ');
};

/*
 * @description 全局过滤器：将空字符串转成--
 * @param {string} str 字符串
 * @returns {string} --
 */
export const lineString = (str) => {
    return typeof str === 'string' && str.trim() !== '' ? str : '--';
};

/*
 * @description 全局过滤器：判断 enable 或 disable
 * @param {string} str 字符串
 * @returns {string} enable 或 disable
 */
export const enableOrDisable = (val) => {
    return val == '1' ? i18n.t('common.enableLable') : i18n.t('common.disableLable');
};
